package com.fairytask.ui
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.checkBox.CheckBox;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.text.Text;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.BagItemBak;
	import com.fairytask.ui.item.InfoItem;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * 弹出窗口 
	 * @author Navy
	 */
	public class InfoUI extends FrameUI
	{
		public static var instance	:InfoUI;
		
		//显示对象
		public var maskMc		:MovieClip;
		public var mainBg		:MovieClip;
		public var mainSpace	:Sprite	=	new Sprite();
		public var txtSpace		:Sprite	=	new Sprite();
		public var btnSpace		:Sprite	=	new Sprite();
		
		public var titleTxt		:Label; //信息文字
		public var infoLabel	:Text; //信息文字
		public var txtBg		:Sprite; //文字背景
		public var btnConfirm	:Button; //确定
		public var btnCancel	:Button; //取消
		public var btnMax		:Button; //最大化 计算...
		private var _autoHide:Boolean	=	false;		//是否自动销毁
		private var _time		:Timer;
		public var hideCallBack:Function;//关闭回调
		public var hideCallBackParams:Array=[];//回调参数
		//勾选框
		public var checkSp		:Sprite	=	new Sprite();
		public var checkBox	:CheckBox;
		public var checkLab	:Label;
		//物品框
		public var itemSp		:Sprite	=	new Sprite();
		public var itemMc		:BagItemBak	=	new BagItemBak();
		public var itemLab		:Label	=	new Label();
		//input动态创建吧
		private var _itemPosArr	:Array	=	[15,45];
		//数据
		private var _data		:Array	=	[];
		//提供几个常用数据
		public static const CHOICEBTN	:Array	=	[StaticIni.getSwfLangStr("1000000372"),StaticIni.getSwfLangStr("4002000120")];
		public static const SINGLEBTN	:Array	=	[StaticIni.getSwfLangStr("1000000372")];
		public static const INFOTXT		:String	=	StaticIni.getSwfLangStr("1000000373");
		public static const SELLTXT		:String	=	StaticIni.getSwfLangStr("1000000374");
		
		
		public static function getInstance():InfoUI
		{
			if(instance==null)
			{
				instance	=	new InfoUI();
			}
			return instance;
		}
		
		public function InfoUI()
		{
			
		}
		
		override public function initGUI():void
		{
			windowBG.removeDrag();
			windowBG.removeClose();
			windowBG.addChild(mainSpace);
			//物品框初始化
			itemSp.x	=	90;
			itemSp.y	=	40;
			itemSp.addChild(itemMc);
			//初始化位置
			initPos();
			_time	=	new Timer(1000,60);
			_time.addEventListener(TimerEvent.TIMER_COMPLETE,onTimeUp);
		}
		
		/**
		 *	自动销毁 
		 * 
		 */		
		private function onTimeUp(evt:TimerEvent):void
		{
			this.hide();
			//自销毁回调函数存在,执行回调
			if(_callBackArr[2])
			{
				_callBackArr[2].call(this,_data);
			}
		}
		
		/**
		 * 带物品框的确认框 
		 * @param txt
		 * @param btnArr
		 * @param callArr
		 * @param hasCheck 是否有勾选框
		 * 
		 */
		public function txtWithItem(txt:String,btnArr:Array,callArr:Array,configID:int,itemNum:int,hasCheck:Boolean=false):void
		{
			//清除显示
			clear();
			infoLabel.color		=	"FFCB4A";
			infoLabel.htmlStr	=	txt;
			infoLabel.filters	=	[Filter.COMMON_LABEL_FILTER];
			mainSpace.addChild(txtSpace);
			//物品信息
			itemMc.configID = configID;
			itemLab.htmlStrByBorder = StaticIni.getSwfLangStrVar("1000000569",[StaticIni.getSwfLangStr(String(configID)),itemNum]);
//			itemLab.x = itemLab.width/2;
			mainSpace.addChild(itemSp);
			//ui大小调整
			btnSpace.y	=	155;
			btnSpace.y	=	155;
			mainBg.height	= 225;
			
			placeBtn(btnArr,callArr);
			//勾选框
			if(hasCheck)
			{
				checkSp.y	=	120;
				mainSpace.addChild(checkSp);
			}
			
			show();
		}
		
		/**
		 * 只有文本和按钮的消息框 
		 * @param txt
		 * @param btnArr
		 * @param callArr
		 * @param titleStr 如果为空，不显示标题
		 * @param hasCheck 是否有勾选框
		 * 
		 */
		public function txtWithBtn(txt:String,btnArr:Array,callArr:Array,titleStr:String=null,pData:Array	=null,hasCheck:Boolean=false):void
		{
			//清除显示
			clear();
			_data	=	pData;
			infoLabel.color		=	"FFCB4A";
			infoLabel.htmlStr	=	txt;
			infoLabel.filters	=	[Filter.COMMON_LABEL_FILTER];
			mainSpace.addChild(txtSpace);
			placeBtn(btnArr,callArr);
			//勾选框
			if(hasCheck)
			{
				mainSpace.addChild(checkSp);
			}
			else
			{
				infoLabel.y	=	20;
			}
			if(titleStr!=null&&titleStr!="")
			{
				placeTitle(titleStr);
			}
			
			show();
		}
		
		/**
		 * 带说明的输入框 只有一个输入框
		 * @param txt
		 * @param InfoItem [string,input,maxNum]
		 * @param btnArr
		 * @param callArr
		 * @param titleStr
		 * 
		 */
		public function inputWithDesc(txt:String,input:InfoItem,btnArr:Array,callArr:Array,titleStr:String=null):void
		{
			//清除显示
			clear();
			
			if(titleStr!=null&&titleStr!="")
			{
				placeTitle(titleStr);
			}
			infoLabel.htmlStr	=	txt;
			infoLabel.color		=	"FFCB4A";
			infoLabel.filters	=	[Filter.COMMON_LABEL_FILTER];
			mainSpace.addChild(txtSpace);
			mainSpace.addChild(input);
			input.y	=	40;
			input.x = 	(mainBg.width - 40 - input.width)/2;
			input.doCall()
			//按钮
			placeBtn(btnArr,callArr);
			
			//显示
			show();
		}
		
		/**
		 * 不带说明的输入框 
		 * @param inputArr[InfoItem...]
		 * @param btnArr
		 * @param callArr
		 * @param extraBtn
		 * @param titleStr
		 */
		public function inputWithoutDesc(inputArr:Array,btnArr:Array,callArr:Array,titleStr:String=null):void
		{
			//清除显示
			clear();
			
			if(titleStr!=null&&titleStr!="")
			{
				placeTitle(titleStr);
			}
			var tmpInput:InfoItem;
			if(inputArr.length>1)
			{
				for(var i:int=0;i<2;i++)
				{
					mainSpace.addChild(inputArr[i]);
					inputArr[i].y	=	_itemPosArr[i];
					inputArr[i].doCall();
				}
			}
			else
			{
				tmpInput	=	inputArr[0];
				mainSpace.addChild(tmpInput);
				tmpInput.y	=	35;
			}
			//按钮
			placeBtn(btnArr,callArr);
			//显示
			show();
		}
		
		/**
		 * 每次显示的时候都要clear 
		 */
		private function clear():void
		{
			_data	=	[];
			_callBackArr	=	[];
			while(mainSpace.numChildren>0)
			{
				mainSpace.removeChildAt(0);
			}
			//勾选框默认不选
			if(checkBox.selected)
			{
				checkBox.selected	=	false;
			}
			initPos();
		}
		
		/**
		 * 默认位置 
		 * 
		 */
		private function initPos():void
		{
			//按钮位置
			btnSpace.y	=	95;
			//勾选框位置
			checkSp.x	=	10;
			checkSp.y	=	60;
			//信息文字
			infoLabel.y	=	5;
			//底图高度
			mainBg.height	= 165;
		}
		
		/**
		 * 放置标题 
		 * @param str
		 */		
		private function placeTitle(str:String):void
		{
//			titleTxt.htmlStrByBorder	=	str;
//			titleTxt.x	=	(280-titleTxt.width)/2;
		}
		
		/**
		 * 放置按钮 
		 * @param btnArr
		 * @param callArr
		 * 
		 */
		private var _callBackArr	:Array	=	[];
		private function placeBtn(btnArr:Array,callArr:Array):void
		{
			btnConfirm.removeEventListener(MouseEvent.CLICK,comfirmEvent);
			btnCancel.removeEventListener(MouseEvent.CLICK,cancelEvent);
			
			_callBackArr	=	callArr;
			while(btnSpace.numChildren>0)
			{
				btnSpace.removeChildAt(0);
			}
			if(btnArr.length>1)
			{
//				btnConfirm.label	=	btnArr[0];
//				btnCancel.label		=	btnArr[1];
				btnConfirm.x		=	(mainBg.width - 40)/4-btnConfirm.width/2;
				btnCancel.x			=	mainBg.width - 40 -btnConfirm.x-btnConfirm.width;
				btnSpace.addChild(btnConfirm);
				btnSpace.addChild(btnCancel);
				btnConfirm.addEventListener(MouseEvent.CLICK,comfirmEvent);
				
				btnCancel.addEventListener(MouseEvent.CLICK,cancelEvent);
			}
			else
			{
//				btnConfirm.label	=	btnArr[0];
				btnConfirm.x		=	(mainBg.width - 40)/2-btnConfirm.width/2;
				btnSpace.addChild(btnConfirm);
				btnConfirm.addEventListener(MouseEvent.CLICK,comfirmEvent);
			}
			mainSpace.addChild(btnSpace);
		}
		
		private function comfirmEvent(evt:MouseEvent):void
		{
			hide();
			btnConfirm.removeEventListener(MouseEvent.CLICK,comfirmEvent);
			if(_callBackArr[0]!=null)
			{
				_callBackArr[0].call(this,_data);
			}
		}
		
		private function cancelEvent(evt:MouseEvent):void
		{
			hide();
			btnCancel.removeEventListener(MouseEvent.CLICK,cancelEvent);
			if(_callBackArr[1]!=null)
			{
				_callBackArr[1].call(this,_data);
			}
		}
		
		override public function show(space:String=MIDDLELAYER, onshow:Function=null):void
		{
			super.show(LOADLAYER,onshow);
			if(_autoHide)
			{
				_time.reset();
				_time.start();
			}
			
		}
		override public function hide(e:*=null):void
		{
			super.hide(e);
			_autoHide	=	false;
			if(hideCallBack!=null)
			{
				hideCallBack.call(this,hideCallBackParams);
			}
		}

		/**
		 *	是否自动隐藏 
		 * @return 
		 * 
		 */		
		public function get autoHide():Boolean
		{
			return _autoHide;
		}

		public function set autoHide(value:Boolean):void
		{
			_autoHide = value;
			
		}
		
		/**
		 * 返回，勾选框是否选中 
		 * @return 
		 * 
		 */
		public function get isCheck():Boolean
		{
			return checkBox.selected;
		}
		
		/**
		 * 场景变化改变位置 
		 * 
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			//调整遮罩位置
			maskMc.width = GameStage.stageWidth + 10;
			maskMc.height = GameStage.stageHeight + 10;
			maskMc.x	=	initX - GameStage.mainLayer.x - 775;
			maskMc.y 	= 	initY - GameStage.mainLayer.y - 385;
		}

	}
}